<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj: {
					//查询关键字
					query: '',
					//商品分类id
					cid: '',
					//页码值
					pagenum: 1,
					//每页显示多少条数据
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				//节流阀
				isLoading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			
			// 调用获取商品列表数据的方法
			  this.getGoodsList()
		},
		methods: {
			async getGoodsList(cb) {
				//开启节流阀
				this.isLoading = true
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				if(res.meta.status !== 200) return uni.$showMsg()
				//关闭节流阀
				this.isLoading = false
				cb && cb() //回调函数
				// this.goodsList = res.message.goods
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(goods) {
				uni.navigateTo({
					url: '../goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		//触底事件
		onReachBottom() {
			//判断是否还有下一页
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg("数据已经加载完成!")
			
			//是否开启节流阀
			if (this.isLoading) return
			//页码自增1
			this.queryObj.pagenum++
			
			this.getGoodsList()
		},
		//下拉刷新数据
		onPullDownRefresh() {
			//重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			
			//重新调用数据 cb
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
	
</style>
